
import { _decorator, Component, Node, Collider, ITriggerEvent } from 'cc';
import { Constant } from '../framework/Constant';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;


@ccclass('Bullet')
export class Bullet extends Component {
  
   public _bulletSpeed=0;
   private  _isEnemyBullet=false;
   private  _Direction=Constant.Direction.MIDDLE;
 
    onEnable () {
        const  collider=this.getComponent(Collider);
        collider.on('onTriggerEnter',this._onTriggerEnter,this);
    }

    onDisable () {
        const  collider=this.getComponent(Collider);
        collider.off('onTriggerEnter',this._onTriggerEnter,this);
    }

    update (deltaTime: number) {
        const  pos=this.node.position;
        let moveLenght=0;
        if(this._isEnemyBullet)
        {
           moveLenght=pos.z+this._bulletSpeed;
           this.node.setPosition(pos.x,pos.y,moveLenght);
             if(moveLenght>50)
             {
                PoolManager.instance().putNode(this.node);
                // this.node.destroy();
                /// console.log("bullte destory");
             }
          
        }else
        {
         moveLenght=pos.z-this._bulletSpeed;
         if(this._Direction===Constant.Direction.LEFT)
         {
            this.node.setPosition(pos.x-this._bulletSpeed*0.2,pos.y,moveLenght);
         }
         else if(this._Direction===Constant.Direction.RIGHT)
         {
            this.node.setPosition(pos.x+this._bulletSpeed*0.2,pos.y,moveLenght);
            console.log('PODSSSSSSl:',pos.x+this._bulletSpeed*0.2);
         }else
         {
            this.node.setPosition(pos.x,pos.y,moveLenght);
         }
        
           if(moveLenght<-50)
           {
               //this.node.destroy();
               PoolManager.instance().putNode(this.node);
               console.log("bullte destory");
           }
        }
        
    }

    show(speed:number,isEnemyBullet:boolean,direction=Constant.Direction.MIDDLE)
    {
        this._Direction=direction;
        this._bulletSpeed=speed;
        this._isEnemyBullet=isEnemyBullet;
    }

    private _onTriggerEnter(event:ITriggerEvent)
    {
     
        PoolManager.instance().putNode(this.node);
       // this.node.destroy();
    }
}
/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
